<?php
namespace Modules\UI\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\UI\Entities\Danmu;

use Setting;
use Validator;
use ThrowException;
use DB;
use Cache;

use Modules\Core\Services\ImportService;

class DanmuController extends Controller
{

    public function statusTotal(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $total = [
            'off_stock' => Danmu::offStock()->count(),
            'on_stock' => Danmu::onStock()->count(),
        ];

        return [
            'total' => $total
        ];
    }


    // 解析导入的文件
    public function importFile(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $mapKey = [
            'user_name',
            'user_headimg',
            'sku_type',
            'sku_id',
            'emited_at'
        ];

        $arr = ImportService::mapArray($req->file, false, $mapKey);

        $uuid = uniqid();
        $cached = [
            'list' => $arr
        ];
        Cache::put($uuid, $cached, 300);

        return [
            'total' => count($arr),
            'uuid' => $uuid
        ];
    }

    // 确认导入
    public function importConfirm(Request $req)
    {
        $rule = [
            'uuid' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $cached = Cache::get($req->uuid) ?: ThrowException::Conflict('缓存已失效，请重新上传');

        $arr = [];

        $mockNumber = date('mdHis');

        foreach ($cached['list'] as $item) {
            if (!@$item['sku_type']) {
                continue;
            }
            $class = config('map.base_sku_type')[$item['sku_type']];

            $sku = $class::getEntity([
                'id' => $item['sku_id']
            ]);

            $node = $sku->activity;

            array_push($arr, [
                'uuid' => uniqid(),
                'user_name' => @$item['user_name'],
                'user_headimg' => @$item['user_headimg'],
                'source_type' => 2,
                'mock_number' => $mockNumber,
                'node_type' => @$item['node_type'],
                'sku_id' => @$item['sku_id'],
                'sku_title' => $sku->title,
                'node_type' => $node->getType('node_type'),
                'node_uuid' => $node->uuid,
                'node_title' => $node->title,
                'node_id' => $node->id,
                'emited_at' => @$item['emited_at'],
            ]);
        }

        Danmu::insert($arr);

        return [];

    }


    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();

        $list = Danmu::getList($options);

        return $list->generateListResponse();
    }

    public function store(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $info = $req->all();

        $item = Danmu::create($info);

        return [
            'id' => $item->id,
            'uuid' => $item->uuid
        ];
    }

    public function show(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Danmu::getEntity([
            'uuid' => $uuid
        ]);

        $info = $item->getInfo();

        return [
            'info' => $info
        ];
    }

    public function update(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Danmu::getEntity([
            'uuid' => $uuid
        ]);

        if ($req->type === 'update') {
            $info = $req->input('attributes');
            $item->update($info);
        }
        elseif ($req->type  === 'set_top')  {
            // 置顶
            $item->update([
                'set_top_at' => now()
            ]);
        }
        elseif ($req->type  === 'unset_top')  {
            // 取消置顶
            $item->update([
                'set_top_at' => NULL
            ]);
        }

        return [];
    }

    public function destroy(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Danmu::getEntity([
            'uuid' => $uuid
        ]);

        $item->delete();

        return [];
    }
}
